﻿// $Id$
/*! \file
 * \brief Definition of Northwind.DomainModel.Customer.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataRules;
#if (!NET_CF)
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
#endif

namespace Northwind.DomainModel
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary />
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial interface ICustomerContainer
    {
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary />
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial interface ICustomerMapper:
        IDataMapper
    {
    }



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary />
    ///
    ///////////////////////////////////////////////////////////////////////////

#if (!NET_CF)
    [Serializable]
    [DataObject]
#endif
    public partial class Customer:
        DomainModel<Customer.PrimaryKey, Customer.DataTransfer>
    {

        /// <summary />
#if (!NET_CF)
        [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
#endif
        public partial class PrimaryKey:
            PrimaryKey<string>
        {

            /// <summary />
            public PrimaryKey():
                base()
            {
            }

            /// <summary />
            public PrimaryKey(string key):
                base(key)
            {
            }

        }

        /// <summary />
        [SoapType("CustomerData", "http://www.codeplex.com/salamanca/northwind/domainmodel/")]
        [XmlType("CustomerData", Namespace="http://www.codeplex.com/salamanca/northwind/domainmodel/")]
#if (!NET_CF)
        [Serializable]
        [SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
#endif
        public partial class DataTransfer:
            IDataTransferObject
        {
        }

        /// <summary />
        public Customer():
            base()
        {
        }

        /// <summary />
        internal Customer(DataMapperCollection dataMappers):
            base(dataMappers)
        {
        }

        /// <summary />
        public Customer(string customerID, DataMapperCollection dataMappers):
            base(dataMappers)
        {
            Debug.Assert(customerID!=null);
            if (customerID==null)
                throw new ArgumentNullException("customerID");

            Data.CustomerID=customerID;
        }

#if (!NET_CF)
        /// <summary />
        protected Customer(SerializationInfo info, StreamingContext context):
            base(info, context)
        {
        }
#endif

        /// <summary />
        protected override void OnDeleting(PersistenceEventArgs e)
        {
            base.OnDeleting(e);

            // Composition
            _LoadOrders(true);
            _Orders.Delete(e.Context);
        }

        /// <summary />
        public override void Load(bool lazyLoad)
        {
            base.Load(lazyLoad);

            if (!IsNew)
            {
                if (!lazyLoad)
                    _LoadOrders(false);
            }
        }

        /// <summary />
        public override void Unload()
        {
            base.Unload();

            _Orders=null;
        }

        /// <summary />
        private void _LoadOrders(bool lazyLoad)
        {
            if (_Orders==null)
            {
                if (IsNew)
                    _Orders=new DomainModelKeyedCollection<Order>();
                else
                    _Orders=(DomainModelKeyedCollection<Order>)Order.FindByCustomer(this, DataMappers);

                _Orders.Saving+=delegate(object s, DomainModelCollectionSavingEventArgs<Order> e) {
                    foreach (Order o in e.AddedItems)
                        o.Customer=this;
                    foreach (Order o in e.RemovedItems)
                        if (o.Customer==this)
                            o.Delete(e.Context);
                };
            }
        }

        /// <summary />
        [StringLengthRule(5, MinLength=5, Description="The company ID must be 5 characters long.")]
        [NotNullRule]
#if (!NET_CF)
        [DataObjectField(false, false, false)]
#endif
        public string CustomerID
        {
            get
            {
                return Data.CustomerID;
            }
        }

        /// <summary />
        [StringLengthRule(40)]
        [NotNullRule]
#if (!NET_CF)
        [DataObjectField(false, false, false)]
#endif
        public string CompanyName
        {
            get
            {
                return _CompanyNameProperty;
            }
            set
            {
                if (_CompanyNameProperty!=value)
                {
                    _CompanyNameProperty=value;
                    RaisePropertyChanged("CompanyName");
                }
            }
        }

        /// <summary />
        [StringLengthRule(30)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string ContactName
        {
            get
            {
                return _ContactNameProperty;
            }
            set
            {
                if (_ContactNameProperty!=value)
                {
                    _ContactNameProperty=value;
                    RaisePropertyChanged("ContactName");
                }
            }
        }

        /// <summary />
        [StringLengthRule(30)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string ContactTitle
        {
            get
            {
                return _ContactTitleProperty;
            }
            set
            {
                if (_ContactTitleProperty!=value)
                {
                    _ContactTitleProperty=value;
                    RaisePropertyChanged("ContactTitle");
                }
            }
        }

        /// <summary />
        [StringLengthRule(60)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string Address
        {
            get
            {
                return _AddressProperty;
            }
            set
            {
                if (_AddressProperty!=value)
                {
                    _AddressProperty=value;
                    RaisePropertyChanged("Address");
                }
            }
        }

        /// <summary />
        [StringLengthRule(15)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string City
        {
            get
            {
                return _CityProperty;
            }
            set
            {
                if (_CityProperty!=value)
                {
                    _CityProperty=value;
                    RaisePropertyChanged("City");
                }
            }
        }

        /// <summary />
        [StringLengthRule(15)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string Region
        {
            get
            {
                return _RegionProperty;
            }
            set
            {
                if (_RegionProperty!=value)
                {
                    _RegionProperty=value;
                    RaisePropertyChanged("Region");
                }
            }
        }

        /// <summary />
        [StringLengthRule(10)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string PostalCode
        {
            get
            {
                return _PostalCodeProperty;
            }
            set
            {
                if (_PostalCodeProperty!=value)
                {
                    _PostalCodeProperty=value;
                    RaisePropertyChanged("PostalCode");
                }
            }
        }

        /// <summary />
        [StringLengthRule(15)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string Country
        {
            get
            {
                return _CountryProperty;
            }
            set
            {
                if (_CountryProperty!=value)
                {
                    _CountryProperty=value;
                    RaisePropertyChanged("Country");
                }
            }
        }

        /// <summary />
        [StringLengthRule(24)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string Phone
        {
            get
            {
                return _PhoneProperty;
            }
            set
            {
                if (_PhoneProperty!=value)
                {
                    _PhoneProperty=value;
                    RaisePropertyChanged("Phone");
                }
            }
        }

        /// <summary />
        [StringLengthRule(24)]
#if (!NET_CF)
        [DataObjectField(false, false, true)]
#endif
        public string Fax
        {
            get
            {
                return _FaxProperty;
            }
            set
            {
                if (_FaxProperty!=value)
                {
                    _FaxProperty=value;
                    RaisePropertyChanged("Fax");
                }
            }
        }

        /// <summary />
        public Collection<Order> Orders
        {
            get
            {
                _LoadOrders(true);
                return new Collection<Order>(_Orders);
            }
        }

        private DomainModelKeyedCollection<Order> _Orders;

    }
}
